<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Music;
use App\Models\MusicParagraph;
use Illuminate\Http\Request;

class MusicParagraphController extends Controller
{
    public function index(Request $request)
    {
        $musicId = $request->input("musicId");

        $models = MusicParagraph::where("music_id", $musicId)->orderBy("music_paragraph_order", "asc")->get();

        return view("admin.musicParagraph.index", [
            "models" => $models,
            "musicId" => $musicId,
            "pageName" => "音乐段落列表",
        ]);
    }

    public function create(Request $request)
    {
        $musicId = $request->input("musicId");

        return view("admin.musicParagraph.create", [
            "pageName" => "添加音乐段落",
            "musicId" => $musicId,

        ]);
    }

    public function save(Request $request)
    {
//        dd($request->post());
        $model = new MusicParagraph();
        $model->music_id = $request->post("music_id");
        $model->music_paragraph_name = $request->post("music_paragraph_name");
        $model->music_paragraph_file = $request->post("music_paragraph_file");
        $model->music_paragraph_order = $request->post("music_paragraph_order");
        $model->save();
        return redirect()->route("adminMusicParagraphIndex", ["musicId" => $model->music_id]);

    }

    public function delete(Request $request)
    {
        $id = $request->input("id");
        Music::destroy($id);
        return redirect()->route("adminMusicParagraphIndex");

    }

    public function edit(Request $request)
    {
        $id = $request->input("id");
        $model = MusicParagraph::find($id);

        return view("admin.musicParagraph.edit", [
            "model" => $model,
            "pageName" => "编辑音乐段落",

        ]);
    }
    public function update(Request $request)
    {
        $id = $request->input("id");
        $model = MusicParagraph::find($id);
        $model->music_paragraph_name = $request->post("music_paragraph_name");
        $model->music_paragraph_file = $request->post("music_paragraph_file");
        $model->music_paragraph_order = $request->post("music_paragraph_order");
        $model->save();
        return redirect()->route("adminMusicParagraphIndex", ["musicId" => $model->music_id]);

    }
}
